package com.artpri.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.transaction.annotation.Transactional;

import com.artpri.model.vo.SimpleGoods;
import com.artpri.service.GoodsFilterService;

/**
 * @author ZhangSi
 */
public class GoodsFilterServiceImpl extends BaseService implements GoodsFilterService{

	private static String sqlHead = "select g.user_num userNum, u.user_name userName, g.goods_num goodsNum, g.goods_name goodsName, g.goods_price goodsPrice"
			+ ", g.goods_leave goodsLeave, g.goods_add_time goodsAddTime, b.ga_id gaId, b.ga_path mainpic from ";
	
	@Override
	@Transactional(readOnly=true)
	public List<SimpleGoods> getFilterResult(Integer rtype, String price,
			String local, Map<Integer, String> attrs) {
		Map<Integer, List<SimpleGoods>> map = getFilterResult(rtype, price, local, attrs, null, null);
		List<SimpleGoods> list = null;
		if (map != null) {
			for (Iterator<Entry<Integer, List<SimpleGoods>>> it = map.entrySet().iterator(); it.hasNext();) {
				Entry<Integer, List<SimpleGoods>> type = (Entry<Integer, List<SimpleGoods>>) it.next();
				list = type.getValue();
			}
		}
		return list;
	}

	@Override
	@Transactional(readOnly=true)
	public Map<Integer, List<SimpleGoods>> getFilterResult(Integer rtype, String price,
			String local, Map<Integer, String> attrs, Integer pageNo, Integer pageSize) {
		
		//构造sql
		String goodSql = "select * from goods where 1=1 ";
		
		if (rtype != null) {
			goodSql += " and gc_id=" + rtype.intValue();
		}
		if (local != null && !local.equals("")) {
			goodSql += " and goods_area='" + local +"'";
		}
		if (price != null && !price.equals("") ) {
			String[] p = price.split("-");
			goodSql += " and goods_price between " + Integer.parseInt(p[0]) + " and " + Integer.parseInt(p[1]);
		}
		
		//移除attrs中value为""的元素
		attrs.remove(null);
		if (attrs != null && attrs.size() != 0) {
			for (Iterator<Entry<Integer, String>> it = attrs.entrySet().iterator(); it.hasNext();) {
				Entry<Integer, String> type = (Entry<Integer, String>) it.next();
				if (type.getValue().equals("")) {
					it.remove();
				}
			}
		}
		
		//保存attrs长度
		int size = attrs.size();
		
		String attrSql = "select DISTINCT a0.goods_num from goods_attr a0 ";
		
		if (size == 1) {
			for (Map.Entry<Integer, String> type : attrs.entrySet()) {
				attrSql += " where a0.attr_id="+type.getKey()+" and a0.ga_value='" + type.getValue() + "'";
			}
		}
		
		if (size > 1) {
			int aKey = 0;
			String aValue = "";
			int i = 0;
			for (Iterator<Entry<Integer, String>> it = attrs.entrySet().iterator(); it.hasNext();) {
				Entry<Integer, String> entry = (Entry<Integer, String>) it.next();
				String alias = "a" + String.valueOf(i);
				if (i == 0) {
					aKey = entry.getKey();
					aValue = entry.getValue();
					i++;
				}else if (i == 1) {
					attrSql += "join goods_attr a1" + " on a0.attr_id=" + aKey + " and a0.ga_value='" + aValue + "'"
							+ " and a1.attr_id=" + entry.getKey() + " and a1.ga_value='" + entry.getValue() + "'" + " and a1.goods_num = a0.goods_num"; 
					i++;
				}else {
					attrSql += " join goods_attr " + alias + " on " + alias + ".attr_id=" + entry.getKey() + " and " + alias + ".ga_value='" + entry.getValue() + "'" + " and " + alias + ".goods_num = a0.goods_num";
					i++;
				}
			}
		}
		
		String albumSql = "select * from goods_album where ga_main=1";
		
		String sql = "(" + goodSql + ") g, "
				+ "(" + albumSql + ") b, "
				+ "(select user_num, user_name from user_info) u";
		
		if (size != 0) {
			sql += ", (" + attrSql + ") a where g.goods_num=b.goods_num and a.goods_num = g.goods_num and u.user_num=g.user_num";
		}else {
			sql += " where g.goods_num=b.goods_num and u.user_num=g.user_num";
		}
		
		Map<Integer, List<SimpleGoods>> map = new HashMap<Integer, List<SimpleGoods>>();
		Integer num = getSimpleGoodsDao().findCount("select count(*) from " + sql);
		if (pageNo != null && pageNo != 0) {
			
			List<SimpleGoods> list = getSimpleGoodsDao().findByPage(sqlHead + sql, pageNo, pageSize);
			map.put(num, list);
			
			return map;
		}else {
			List<SimpleGoods> list = getSimpleGoodsDao().find(sqlHead + sql);
			map.put(num, list);
			return map;
		}
	}
}
